package com.ehualu.getes.until;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;

public class EsServerUntil {
    private static final Logger LOGGER = LoggerFactory.getLogger(EsServerUntil.class);

    private static RestHighLevelClient client;


    public static void creclient(String host, int port, String schema) {
        String[] ipArr = host.split(",");
        HttpHost[] httpHosts = new HttpHost[ipArr.length];
        for (int i = 0; i < ipArr.length; i++) {
            httpHosts[i] = new HttpHost(ipArr[i], port, schema);
        }
        RestClientBuilder builder = RestClient.builder(httpHosts);
        client = new RestHighLevelClient(builder);

    }


    public static void getshij2(String starttime, String endTimeStr, String index, String kkid) {
        try {

            System.out.println("********EsStaticUtil**********");
            String indexName = index;

            SearchRequest searchRequest = new SearchRequest(indexName);
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//			sourceBuilder.from(0); // 默认0
            sourceBuilder.size(100);// 默认10条

            QueryBuilder queryBuilder = QueryBuilders.boolQuery()
                    .must(QueryBuilders.termQuery("caddresscode", kkid))
                    .must(QueryBuilders.rangeQuery("createtime").from(starttime).to(endTimeStr));
            sourceBuilder.query(queryBuilder);
            searchRequest.source(sourceBuilder);
            SearchResponse response = client.search(searchRequest);
            SearchHits hits = response.getHits();
            SearchHit[] searchHits = hits.getHits();
            int sl = searchHits.length;
            System.out.println("查询数据量：" + sl);
            for (SearchHit hit : searchHits) {


                Map<String, Object> sourceAsMap = hit.getSourceAsMap();// 获取查询结果json

                String strccarnumber = (String) sourceAsMap.get("ccarnumber");
                String strid = (String) sourceAsMap.get("dtid");

                System.out.println("strccarnumber:" + strccarnumber);
                System.out.println("strid:" + strid);
//				log.info("索引：" + index + ",类型：" + type + ",ID：" + id + ",得分：" + score + "," + sourceAsMap);
//				return sourceAsMap.get("pic1") + "";
            }
            System.out.println("循环*****结束");

//			return pic;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }






    public static void close() {
        if (client != null) {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
